package com.drug.web.controller.wms;

import com.drug.common.annotation.Log;
import com.drug.common.constant.HttpStatus;
import com.drug.common.core.controller.BaseController;
import com.drug.common.core.domain.AjaxResult;
import com.drug.common.core.page.TableDataInfo;
import com.drug.common.enums.BusinessType;
import com.drug.common.exception.ServiceException;
import com.drug.common.utils.poi.ExcelUtil;
import com.drug.wms.domain.BusSequenceConf;
import com.drug.wms.service.IBusSequenceConfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 序列配置Controller
 *
 * @author ruoyi
 * @date 2021-11-04
 */
@RestController
@RequestMapping("/wms/sequence")
public class BusSequenceConfController extends BaseController {
    @Autowired
    private IBusSequenceConfService busSequenceConfService;

    /**
     * 查询序列配置列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BusSequenceConf busSequenceConf) {
        startPage();
        List<BusSequenceConf> list = busSequenceConfService.selectBusSequenceConfList(busSequenceConf);
        return getDataTable(list);
    }

    /**
     * 导出序列配置列表
     */
    @Log(title = "序列配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BusSequenceConf busSequenceConf) {
        List<BusSequenceConf> list = busSequenceConfService.selectBusSequenceConfList(busSequenceConf);
        ExcelUtil<BusSequenceConf> util = new ExcelUtil<BusSequenceConf>(BusSequenceConf.class);
        return util.exportExcel(list, "序列配置数据");
    }

    /**
     * 获取序列配置详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(busSequenceConfService.selectBusSequenceConfById(id));
    }

    /**
     * 新增序列配置
     */
    @Log(title = "序列配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusSequenceConf busSequenceConf) {
        //判断编码唯一
        BusSequenceConf list = busSequenceConfService.selectBusSequenceConfByCode(busSequenceConf.getCode());
        if (list != null) {
            throw new ServiceException("当前编码已存在", HttpStatus.ERROR);
        }
        return toAjax(busSequenceConfService.insertBusSequenceConf(busSequenceConf));
    }

    /**
     * 修改序列配置
     */
    @Log(title = "序列配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusSequenceConf busSequenceConf) {
        return toAjax(busSequenceConfService.updateBusSequenceConf(busSequenceConf));
    }

    /**
     * 删除序列配置
     */
    @Log(title = "序列配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(busSequenceConfService.deleteBusSequenceConfByIds(ids));
    }

    /**
     * 通过code获取唯一编号
     */
    @GetMapping("/seq/{code}")
    public AjaxResult genSeqByCode(@PathVariable String code) {
        return AjaxResult.success(busSequenceConfService.genSeqByCode(code));
    }
}
